package com.lr.wfxmanager.merchant.service.impl;

import com.github.wxpay.sdk.MyPayConfig;
import com.github.wxpay.sdk.WXPay;
import com.lr.wfxmanager.bean.WxbGood;
import com.lr.wfxmanager.bean.WxbOrder;


import com.lr.wfxmanager.merchant.mapper.MerchantMapper;
import com.lr.wfxmanager.merchant.service.IMerchantService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

@Service
public class MerchantServiceImpl implements IMerchantService {
    @Resource
    private MerchantMapper merchantMapper;

    @Override
    public String addOrder(WxbOrder order) throws Exception {
        Random random = new Random();
        int nextInt = random.nextInt(1000000);
        //创建订单号
        String orderId = "lr"+nextInt;
        order.setOrderId(orderId);
        order.setUserId("69609206");//自媒体id
        order.setOrderTime(new Timestamp(System.currentTimeMillis()));
        merchantMapper.addOrder(order);//平台创建订单

        //拉取微信支付
        try {
            WXPay wxPay = new WXPay(new MyPayConfig());
            Map<String, String> data = new HashMap<String, String>();
            data.put("body", "微分销下单");
            data.put("out_trade_no", orderId);
            data.put("device_info", "PC");
            data.put("fee_type", "CNY");
            data.put("total_fee", "1");
            data.put("spbill_create_ip", "123.12.12.123");
            //支付成功之后的回调接口
            //微信告诉微分销商户订单已付款
            data.put("notify_url", "http://6vk2fm.natappfree.cc/merchant/notify_url");
            data.put("trade_type", "NATIVE");  // 此处指定为扫码支付
            data.put("product_id", "12");

            try {
                //下单
                Map<String, String> resp = wxPay.unifiedOrder(data);
                System.out.println(resp.get("code_url"));
                //返回的resp包含二维码
                String code_url = resp.get("code_url");
                return code_url;
            } catch (Exception e) {
                e.printStackTrace();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    //修改订单状态
    @Override
    public void updateOrderState(Integer state, String orderId)throws Exception {
        merchantMapper.updateOrderState(state,orderId);
    }
    /**
     * 存在事务:当商品未添加成功,方案是不能添加的
     * customer_id自定义,good_id自动生成,skuid自动生成
     * @param goodsBean
     */
    @Transactional
    @Override
    public void addGoods(WxbGood goodsBean) throws Exception {
        Random random = new Random();
        int nextInt = random.nextInt(100000);
        String good_id = "gd"+nextInt;//商品id
        String sku_id = "sku"+nextInt;
        String customer_id="69609206";//商家id
        goodsBean.setGood_id(good_id);
        goodsBean.setCustomer_id(customer_id);
        /*添加商品*/
        merchantMapper.addGoods(goodsBean);
        List<WxbGood.TaocanArrayEntity> taocanArray = goodsBean.getTaocanArray();
        if (null != taocanArray && 0 != taocanArray.size()){
            //方案不为空
            int i = 1;
            for (WxbGood.TaocanArrayEntity taocan:taocanArray) {
                taocan.setGoods_id(good_id);
                taocan.setSku_id(sku_id+i);
                i++;
            }
            /*添加方案*/
            merchantMapper.addSuks(taocanArray);
        }


    }
}
